result_csv_file=$1
raw_bench_dir=/public/home/jwli/workSpace/yongkang/AAAI-24July-labs/our_benchmarks/jacm_artifact/benchmarks-reduced
# new_bench_dir=/public/home/jwli/workSpace/yongkang/24Sep-ltlfsyn-labs/experiments_from_24.1014/tmp_bench
new_bench_dir=$2
suffix=$3
mkdir -p $new_bench_dir


result_folder_name=$(basename $(dirname $result_csv_file))
new_bench_dir=$new_bench_dir/$result_folder_name"_tmp_bench"$suffix
if [ -d "$new_bench_dir" ]; then
    echo $new_bench_dir' exists'
    exit 1
fi
lab_results_parser=/public/home/jwli/workSpace/yongkang/24Sep-ltlfsyn-labs/experiments_from_24.1014/lab_results_parser
cd $lab_results_parser
diff_res_file=$(mktemp /tmp/mytempfile.XXXXXX)
result_csv_compared_file=/public/home/jwli/workSpace/yongkang/24Sep-ltlfsyn-labs/experiments_from_24.1014/lab_results_parser/experiment_results/experiment_raw_csv-30min_24.0815/output_syntcomp23_tople-m1e1.csv
python diff/unique.py $result_csv_file $result_csv_compared_file > $diff_res_file
cases_list_file=$(mktemp /tmp/mytempfile.XXXXXX)
grep -oP '[\w_-]*/[\w_]*' "$diff_res_file" | sort > "$cases_list_file"
rm $diff_res_file

# declare -g used_folder_list=()

folder_list=()
folder_list+=("Patterns/GFand")
folder_list+=("Patterns/Uright")
folder_list+=("Random/Syft/syft_1")
folder_list+=("Random/Syft/syft_2")
folder_list+=("Random/Syft/syft_3")
folder_list+=("Random/Syft/syft_4")
folder_list+=("Random/Syft/syft_5")
folder_list+=("Random/Lydia/case_03_50")
folder_list+=("Random/Lydia/case_04_50")
folder_list+=("Random/Lydia/case_05_50")
folder_list+=("Random/Lydia/case_06_50")
folder_list+=("Random/Lydia/case_07_50")
folder_list+=("Random/Lydia/case_08_50")
folder_list+=("Random/Lydia/case_09_50")
folder_list+=("Random/Lydia/case_10_50")
folder_list+=("Two-player-Game/Single-Counter/System-first")
folder_list+=("Two-player-Game/Double-Counter/System-first")
folder_list+=("Two-player-Game/Nim/nim_01/System-first")
folder_list+=("Two-player-Game/Nim/nim_02/System-first")
folder_list+=("Two-player-Game/Nim/nim_03/System-first")
folder_list+=("Two-player-Game/Nim/nim_04/System-first")
folder_list+=("Two-player-Game/Nim/nim_05/System-first")
folder_list+=("Two-player-Game/Nim/nim_06/System-first")
folder_list+=("Two-player-Game/Nim/nim_07/System-first")
folder_list+=("Two-player-Game/Nim/nim_08/System-first")
folder_list+=("Two-player-Game/Nim/nim_09/System-first")
folder_list+=("Two-player-Game/Nim/nim_10/System-first")
folder_list+=("Two-player-Game/Nim/nim_11/System-first")
folder_list+=("Two-player-Game/Nim/nim_12/System-first")
folder_list+=("Two-player-Game/Nim/nim_13/System-first")
folder_list+=("Two-player-Game/Nim/nim_14/System-first")
folder_list+=("Two-player-Game/Nim/nim_15/System-first")
folder_list+=("Two-player-Game/Nim/nim_16/System-first")
folder_list+=("Two-player-Game/Nim/nim_17/System-first")
folder_list+=("Two-player-Game/Nim/nim_18/System-first")
folder_list+=("Two-player-Game/Nim/nim_19/System-first")
folder_list+=("Two-player-Game/Nim/nim_20/System-first")
folder_list+=("Ascending/tree_size_1")
folder_list+=("Ascending/tree_size_2")
folder_list+=("Ascending/tree_size_3")
folder_list+=("Ascending/tree_size_4")
folder_list+=("Ascending/tree_size_5")
folder_list+=("Ascending/tree_size_6")
folder_list+=("Ascending/tree_size_7")
folder_list+=("Ascending/tree_size_8")
folder_list+=("Ascending/tree_size_9")

folder_ids=()
folder_ids+=("GFand")
folder_ids+=("Uright")
folder_ids+=("syft_1")
folder_ids+=("syft_2")
folder_ids+=("syft_3")
folder_ids+=("syft_4")
folder_ids+=("syft_5")
folder_ids+=("case_03")
folder_ids+=("case_04")
folder_ids+=("case_05")
folder_ids+=("case_06")
folder_ids+=("case_07")
folder_ids+=("case_08")
folder_ids+=("case_09")
folder_ids+=("case_10")
folder_ids+=("Single-Counter")
folder_ids+=("Double-Counter")
folder_ids+=("nim_01")
folder_ids+=("nim_02")
folder_ids+=("nim_03")
folder_ids+=("nim_04")
folder_ids+=("nim_05")
folder_ids+=("nim_06")
folder_ids+=("nim_07")
folder_ids+=("nim_08")
folder_ids+=("nim_09")
folder_ids+=("nim_10")
folder_ids+=("nim_11")
folder_ids+=("nim_12")
folder_ids+=("nim_13")
folder_ids+=("nim_14")
folder_ids+=("nim_15")
folder_ids+=("nim_16")
folder_ids+=("nim_17")
folder_ids+=("nim_18")
folder_ids+=("nim_19")
folder_ids+=("nim_20")
folder_ids+=("t1")
folder_ids+=("t2")
folder_ids+=("t3")
folder_ids+=("t4")
folder_ids+=("t5")
folder_ids+=("t6")
folder_ids+=("t7")
folder_ids+=("t8")
folder_ids+=("t9")

# Function to map folder_id to folder_list and append the file
function copy_case {
    input_case="$1"  # The case to map, e.g., t2/f3
    
    # Extract folder ID and file name from input
    folder_id=$(echo "$input_case" | cut -d'/' -f1)  # Extract 't2'
    file=$(echo "$input_case" | cut -d'/' -f2)        # Extract 'f3'
    # # Check if the array is empty or if the new element is different from the last one
    # if [[ ${#used_folder_list[@]} -eq 0 || "${used_folder_list[-1]}" != "$folder_id" ]]; then
    #     used_folder_list+=("$folder_id")
    # fi

    # Find the index of the folder ID in folder_ids
    for i in "${!folder_ids[@]}"; do
        if [[ "${folder_ids[$i]}" == "$folder_id" ]]; then
            folder_path="${folder_list[$i]}"
            file_relative_path=$folder_path/$file
            mkdir -p $new_bench_dir/$folder_path
            cp --parent $file_relative_path.* $new_bench_dir
            return
        fi
    done

    # If no match is found, output an error message
    echo "Error: Folder ID '$folder_id' not found in folder_ids."
}

#!/bin/bash

# Declare a global array to store lines
declare -g lines_array=()

# Function to read lines from a file into the array
function read_lines_to_array {
    local file_path="$1"

    # Check if the file exists
    if [[ ! -f "$file_path" ]]; then
        echo "File not found: $file_path"
        return 1
    fi

    # Read the file line by line
    while IFS= read -r line; do
        lines_array+=("$line")  # Append each line to the global array
    done < "$file_path"
}

read_lines_to_array "$cases_list_file"
rm $cases_list_file

cd $raw_bench_dir
# Print the contents of the array
for line in "${lines_array[@]}"; do
    (copy_case $line) &
done
wait

# for folder in "${used_folder_list[@]}"; do
#     echo "$folder"
# done

# 存放匹配的目录的数组
matched_folder_ids=()
matched_folder_list=()

# 遍历 folder_list
for folder_idx in "${!folder_list[@]}"; do
    # 检查目标目录是否存在
    folder="${folder_list[$folder_idx]}"
    folder_id="${folder_ids[$folder_idx]}"
    if [ -d "$new_bench_dir/$folder" ]; then
        # 如果存在，将其添加到 matched_dirs 数组
        matched_folder_ids+=("$folder_id")
        matched_folder_list+=($folder)
    fi
done

# 打印匹配的目录
if [ ${#matched_folder_ids[@]} -gt 0 ]; then
    echo "Matching directories found:"
    for folder_id in "${matched_folder_ids[@]}"; do
        echo "$folder_id"
    done
fi

cd $new_bench_dir
mkdir -p pbs
mkdir -p scripts

echo '
bench_dir=$1
exec_bin=$2

pbs_dir=$bench_dir/pbs
create_pbs_bin=scripts/run_basic/syntcomp23/new_sed2.sh
mkdir -p $pbs_dir
' > scripts/batch_create_pbs.sh
# 遍历 matched_folder_list
for folder_idx in "${!matched_folder_list[@]}"; do
    # 检查目标目录是否存在
    folder="${matched_folder_list[$folder_idx]}"
    folder_id="${matched_folder_ids[$folder_idx]}"
    echo 'sh $create_pbs_bin $bench_dir $exec_bin '$folder'    > $pbs_dir/'$folder_id'.pbs &' >> scripts/batch_create_pbs.sh 
done
echo 'wait' >> scripts/batch_create_pbs.sh

echo '
benchDir=$1
' > scripts/batch_commit_pbs.sh
# 遍历 matched_folder_list
for folder_idx in "${!matched_folder_list[@]}"; do
    # 检查目标目录是否存在
    folder="${matched_folder_list[$folder_idx]}"
    folder_id="${matched_folder_ids[$folder_idx]}"
    echo 'qsub $benchDir/pbs/'$folder_id'.pbs &' >> scripts/batch_commit_pbs.sh 
done
echo 'wait' >> scripts/batch_commit_pbs.sh
